package com.rx.mgt.service;

import com.rx.mgt.dao.EmailBlackDao;
import com.rx.mgt.dao.EmailWhiteListDao;
import com.rx.mgt.domain.BlackListDomain;
import com.rx.mgt.domain.WhiteListDomain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by zxj on 2017/8/15.
 */
@Service
@Transactional
public class EmailWhiteListService {

    @Autowired
    private EmailWhiteListDao whiteListDao;
    @Autowired
    private EmailBlackDao bdao;

    public List<WhiteListDomain> queryBlackList() {
        return (List<WhiteListDomain>)whiteListDao.list();
    }

    public int addWhiteList(String address) {
        BlackListDomain byAddress = bdao.findByAddress(address);
        WhiteListDomain whiteListDomain = whiteListDao.findByAddress(address);
        if(whiteListDomain!=null){
            return 1;
        }else if(byAddress!=null){
            return 2;
        }else  {
            WhiteListDomain white = new WhiteListDomain();
            white.setAddress(address);
            white.setDataTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            white.setStatus("1");
            whiteListDao.save(white);
            return 0;
        }

    }

    public void deleteWhiteList(String id) {
        whiteListDao.delete(whiteListDao.get(Long.valueOf(id)));
    }

    public void upateWhiteList(String id, String flag) {
        WhiteListDomain whiteListDomain = whiteListDao.get(Long.valueOf(id));
        if("0".equals(flag)){
            whiteListDomain.setStatus("0");
        }else{
            whiteListDomain.setStatus("1");
        }
        whiteListDao.update(whiteListDomain);
    }

    public void deleteWhiteListByAddress(String addr) {
        WhiteListDomain byAddress = whiteListDao.findByAddress(addr);
        whiteListDao.delete(byAddress);

    }
}
